<?php
declare(strict_types=1);

namespace SixShop\System;

use think\App;
use think\event\HttpEnd;

class Log extends \think\Log
{
    public function __construct(App $app, private ExtensionManager $extensionManager, private string $extensionID)
    {
        if (!$app->runningInConsole()) {
            $app->event->listen(HttpEnd::class, function () {
                $this->save();
            });
        }
        parent::__construct($app);
    }

    public function getConfig(?string $name = null, $default = null)
    {
        if ($name == 'level') {
            return $this->getLevelConfig();
        }
        return parent::getConfig($name, $default);
    }

    private function getLevelConfig(): array
    {
        $level = parent::getConfig('level', []);
        if (in_array('debug', $level)) {
            return $level;
        }
        $debug = $this->extensionManager->getExtensionConfig($this->extensionID, 'debug');
        if ($debug) {
            $level[] = 'debug';
        }
        return $level;
    }
}